package com.wxwyapp.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import net.sf.json.JSONObject;

import com.frame.ar.hander.DataHander;
import com.frame.ar.kit.ObjKit;
import com.frame.ar.util.MapUtil;
import com.frame.ar.util.TimeUtil;
import com.frame.jfinal.service.BaseService;
import com.frame.util.DateFormatUtil;
import com.jfinal.core.Controller;
import com.justep.baas.data.Table;
import com.weixin.model.AppUser;
import com.wxwyapp.model.Feedback;

/**
 * 作用描述：员客户物业相关业务管理服务层
 * @author LRC
 * @createDate 2015年9月28日
 * @version v1.0 
 * @updateRecord 格式：时间，作者，修改说明
 **
 * 
 */
public class PublicPropertyService extends BaseService{

	/**
	 * 功能描述：用户提交的反馈内容保存
	 * @createDate 2015年10月19日 by LRC
	 * @param publicPropertyController
	 * @return 
	 * @updateRecord 格式：时间，作者，修改说明
	 **
	 * 
	 */
	public static Boolean userCommitFeedBack(
			JSONObject data) {
		
		AppUser user = AppUser.getCurrentUser();
		Feedback fb = new Feedback();
		
		String[] keys = new String[]{"content","type","areaCode","areaName"};
		//复制提交参数
		fb.copyValue(keys , data);
		
		//复制用户信息
		fb.set("personId", user.get("personId"));
		fb.set("personName", user.get("personName"));
		fb.set("personId", user.get("personId"));
		fb.set("telephone", user.get("telephone"));
		fb.set("commitTime", TimeUtil.newTimestamp());
		fb.set("id", UUID.randomUUID().toString());
		
		return fb.save();
	}
	
	public static Table findBizDataForTable(String dataId,Controller c,String[] args){
		//获取用户信息
		AppUser user = AppUser.getCurrentUser();
		if(user==null)return null;
		
		Map<String,Object> base = new HashMap<String,Object>();
		JSONObject params = JSONObject.fromObject(c.getPara("params"));
		MapUtil.copyToMap(params, base, args);
		String personId = user.getStr("personId");
		base.put("personId", personId);
		
		try {
			return (Table) DataHander.getData(dataId, base);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 功能描述： 通过用户加载公告信息
	 * @createDate 2015年10月23日 by LRC
	 * @param c
	 * @return 
	 * @updateRecord 格式：时间，作者，修改说明
	 **
	 * 
	 */
	public static Table findNoticeByUser(Controller c){
		//获取用户信息
		AppUser user = AppUser.getCurrentUser();
		if(user==null)return null;
		
		//获取offset
		String params = c.getPara("params");
		//解析params
		com.alibaba.fastjson.JSONObject jp = com.alibaba.fastjson.JSONObject.parseObject(params);
		
		//获取用户Id
		String personId = user.getStr("personId");
		String columns = "id,title,content,areaCode,areaName,publishTime";//要返回的列
		//查询用户公告
		String sql = null;
		List<Object> sqlParams = new ArrayList<Object>();
		Date time = null;
		if(!ObjKit.isBank(jp.get("publishTime"))){
			time = DateFormatUtil.parseL(jp.getString("publishTime"));
		}
		Integer offset = jp.getInteger("offset");
		
		Map<String, Object> base = new HashMap<String,Object>();
		base.put("personId", personId);
		base.put("offset", offset);
		base.put("publishTime", time==null ? null : new java.sql.Timestamp(time.getTime()));
		
		try {
			Object notices = DataHander.getData("findNotice_byUser", base);
			if(notices instanceof Table){
				return (Table) notices;
			}else{
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		/*
		if(offset==0){
			//加载最新的数据
			if(time==null){
				//加载最新记录
				sql = "select id,title,content,orgCode,areaCode,areaName,publishTime,pid from vw_pms_bi_notice_byuser where publishTime is not null and  pid=? order by publishTime desc";
				sqlParams.add(personId);
			}else{
				//加载大于参数发布时间的数据
				sql = "select id,title,content,orgCode,areaCode,areaName,publishTime,pid from vw_pms_bi_notice_byuser where publishTime is not null and publishTime > ?  and   pid=?  order by publishTime desc";
				sqlParams.add(new java.sql.Timestamp(time.getTime()));
				sqlParams.add(personId);
			}
		}else{
			//加载历史记录
			//加载大于参数发布时间的数据
			sql = "select id,title,content,orgCode,areaCode,areaName,publishTime,pid from vw_pms_bi_notice_byuser where publishTime is not null and publishTime < ?  and   pid=?  order by publishTime desc";
			sqlParams.add(new java.sql.Timestamp(time.getTime()));
			sqlParams.add(personId);
		}
		
		Table table = Util.queryData(DBSource.PMS2_PMS, sql, sqlParams, columns, 0, limit);
		//查询总记录数据
		if(table!=null){
			sql = "select count(1) t from vw_pms_bi_notice_byuser where publishTime is not null  and   pid=?";
			Record r = Db.use(DBSource.PMS2_PMS).findFirst(sql, personId);
			Long total = r.getLong("t");
			table.setTotal(total.intValue());
		}
		return table;
		*/
		
	}

	
	
}
